package com.thirtydays.oral.common.service;

/**
 * TODO
 *
 * @author Dell
 * @version 1.0
 * @date 2021/9/24 14:55
 */
import cn.hutool.core.util.IdUtil;
import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.SqlConnRunner;
import cn.hutool.json.JSONObject;
import java.sql.Connection;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Service
@Profile({"four"})
public class DBServiceFourImpl implements DBService {
    private static final Logger log = LoggerFactory.getLogger(DBServiceFourImpl.class);

    public DBServiceFourImpl() {
    }

    @Override
    public void insert(Connection connection, JSONObject parseObj) {
        log.info("开始处理新增消息：{}", parseObj);

        try {
            SqlConnRunner sqlConnRunner = DbUtil.newSqlConnRunner(connection);
            String realname = parseObj.getStr("realname");
            String gender = parseObj.getStr("gender");
            if ("MALE".equals(gender)) {
                gender = "M";
            } else {
                gender = "F";
            }

            Long birthDate = (Long)parseObj.get("birthDate");
            LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(birthDate / 1000L, 0, ZoneOffset.ofHours(8));
            LocalDate birthTime = localDateTime.toLocalDate();
            String patientId = parseObj.getStr("patientId");
            int insert = sqlConnRunner.insert(connection, Entity.create().setTableName("e2_pat").set("pat_chartno", IdUtil.objectId()).set("pat_lastname", realname).set("pat_birthdate", birthTime).set("pat_gender", gender).set("pat_user_defined_9", patientId));
            log.info("sql 插入成功：,{}", insert);
        } catch (Exception var11) {
            log.error("SQL错误信息: {}", var11.getMessage());
        }

    }

    @Override
    public void update(Connection connection, JSONObject parseObj) {
        log.info("开始处理修改消息：{}", parseObj);

        try {
            SqlConnRunner sqlConnRunner = DbUtil.newSqlConnRunner(connection);
            String realname = parseObj.getStr("realname");
            String gender = parseObj.getStr("gender");
            Long birthDate = (Long)parseObj.get("birthDate");
            String patientId = parseObj.getStr("patientId");
            LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(birthDate / 1000L, 0, ZoneOffset.ofHours(8));
            LocalDate birthTime = localDateTime.toLocalDate();
            if ("MALE".equals(gender)) {
                gender = "M";
            } else {
                gender = "F";
            }

            sqlConnRunner.update(connection, Entity.create().setTableName("e2_pat").set("pat_lastname", realname).set("pat_birthdate", birthTime).set("pat_gender", gender), Entity.create().setTableName("e2_pat").set("pat_user_defined_9", patientId));
        } catch (Exception var10) {
            log.error("SQL错误信息: {}", var10.getMessage());
        }

    }

    @Override
    public void delete(Connection connection, JSONObject parseObj) {
        log.info("开始处理删除消息：{}", parseObj);

        try {
            SqlConnRunner sqlConnRunner = DbUtil.newSqlConnRunner(connection);
            String patientId = parseObj.getStr("patientId");
            sqlConnRunner.del(connection, Entity.create().setTableName("e2_pat").set("pat_user_defined_9", patientId));
        } catch (Exception var5) {
            log.error("SQL错误信息: {}", var5.getMessage());
        }

    }
}
